package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.TravelGroup;
import com.ktgj.customer.domain.TravelGroupDto;
import com.ktgj.customer.domain.TravelGroupListVo;
import com.ktgj.customer.domain.vo.GroupDetailsVo;
import com.ktgj.customer.domain.vo.InsertProductVo;
import com.ktgj.customer.domain.vo.TravelGroupDetailsVo;
import com.ktgj.customer.domain.vo.TravelGroupVo;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.customer.service.ITravelGroupService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 旅游团Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/group")
public class TravelGroupController extends BaseController
{
    @Autowired
    private ITravelGroupService travelGroupService;

    /**
     * 查询旅游团列表
     */
    //@PreAuthorize(hasPermi = "customer:group:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelGroupDto travelGroupDto)
    {
        startPage();
        List<TravelGroupDto> list = travelGroupService.selectTravelGroupList(travelGroupDto);
        return getDataTable(list);
    }

    /**
     * 导出旅游团列表
     */
    //@PreAuthorize(hasPermi = "customer:group:export")
    @Log(title = "旅游团", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelGroupDto travelGroupDto) throws IOException
    {
        List<TravelGroupDto> list = travelGroupService.selectTravelGroupList(travelGroupDto);
        ExcelUtil<TravelGroupDto> util = new ExcelUtil<TravelGroupDto>(TravelGroupDto.class);
        util.exportExcel(response, list, "旅游团数据");
    }

    /**
     * 获取旅游团详细信息
     */
    //@PreAuthorize(hasPermi = "customer:group:query")
//    @GetMapping(value = "/{groupId}")
    @PostMapping("/getGroupInfo")
    public AjaxResult getGroupInfo(@RequestBody Map<String,Long> map)
    {
        if (map.get("groupId") != null && !map.get("groupId").equals(""))
        {
            Long groupId = map.get("groupId");
            return AjaxResult.success(travelGroupService.selectTravelGroupById(groupId));
        }
        return AjaxResult.error("参数不正确！");
    }

    /**
     * 新增旅游团
     */
    //@PreAuthorize(hasPermi = "customer:group:add")
    @Log(title = "旅游团", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelGroupDto travelGroupDto)
    {
        return toAjax(travelGroupService.insertTravelGroup(travelGroupDto));
    }

    /**
     * 修改旅游团
     */
    //@PreAuthorize(hasPermi = "customer:group:edit")
//    @Log(title = "旅游团", businessType = BusinessType.UPDATE)
    @PostMapping("/editGroup")
    public AjaxResult editGroup(@RequestBody TravelGroup travelGroupDto)
    {
        return toAjax(travelGroupService.updateTravelGroup(travelGroupDto));
    }

    /**
     * 删除旅游团
     */
    //@PreAuthorize(hasPermi = "customer:group:remove")
//    @Log(title = "旅游团", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(travelGroupService.deleteTravelGroupByIds(groupIds));
    }

    /**
     * 查询旅游团列表
     * @return
     */
    @GetMapping("/queryGroupList")
    public TableDataInfo queryGroupList() {
        startPage();
        List<TravelGroupListVo> travelGroupListVos = travelGroupService.queryGroupList();
        return getDataTable(travelGroupListVos);
    }

    /**
     * 旅游产品列表
     * */
    @PostMapping("/travelGroupVoList")
    public AjaxResult travelGroupVoList() {
        List<TravelGroupVo> travelGroupVos;
        try {
            travelGroupVos = travelGroupService.travelGroupVoList();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (travelGroupVos != null && travelGroupVos.size() > 0){
            return AjaxResult.success(travelGroupVos);
        }
        else {
            return AjaxResult.error("没有数据");
        }
    }

    /**
     * 旅游产品详情
     * */
    @PostMapping("/travelProductDetails")
    public AjaxResult travelProductDetails(@RequestBody Map<String, Object> map) {
        if (map.get("groupId") != null && !map.get("groupId").equals("")){
            Long groupId = Long.valueOf(map.get("groupId").toString());
            TravelGroupDetailsVo travelGroupDetailsVo;
            try {
                travelGroupDetailsVo = travelGroupService.travelProductDetails(groupId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (travelGroupDetailsVo != null){
                return AjaxResult.success(travelGroupDetailsVo);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查询指定旅游产品成团规则
     * */
    @PostMapping("/selectGroupByGroupId")
    public AjaxResult selectTravelGroupByGroupId(@RequestBody Map<String, Object> map) {
        if (map.get("groupId") != null && !map.get("groupId").equals("")){
            Long groupId = Long.valueOf(map.get("groupId").toString());
            String groupRows;
            try {
                groupRows = travelGroupService.selectTravelGroupByGroupId(groupId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (groupRows != null && !groupRows.equals("")){
                return AjaxResult.success(groupRows);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 旅游填写订单
     * */
    @PostMapping("/groupDetails")
    public AjaxResult groupDetails(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        String setOffTime = map.get("setOffTime").toString();
        String finishTime = map.get("finishTime").toString();
        GroupDetailsVo groupDetailsVo = travelGroupService.groupDetails(request, id, setOffTime, finishTime);
        return AjaxResult.success(groupDetailsVo);
    }

    /**
     * 新建团
     * */
    @PostMapping("/insertProduct")
    public AjaxResult insertProduct(HttpServletRequest request, @RequestBody InsertProductVo insertProductVo){
        int i;
        try {
            i = travelGroupService.insertProduct(request, insertProductVo);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("新增成功");
        }
        else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 查询待修改的团
     * */
    @PostMapping("/updateGroup")
    public AjaxResult updateGroup(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        InsertProductVo insertProductVo = travelGroupService.updateGroup(id);
        return AjaxResult.success(insertProductVo);
    }

    /**
     * 修改团
     * */
    @PostMapping("/group")
    public AjaxResult group(@RequestBody InsertProductVo insertProductVo){
        int i;
        try {
            i = travelGroupService.group(insertProductVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }
}
